<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Forpastime < forpastime@qq.com>
// +----------------------------------------------------------------------
namespace app\tenant_user\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\AdminMenuModel;
use think\Db;
use think\db\Query;

/**
 * Class AdminIndexController
 * @package app\tenant_user\controller
 * @adminMenuRoot(
 *     'name'   =>'商户用户分类',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'user',
 *     'remark' =>'商户用户管理'
 * )
 */
class AdminCategoryController extends AdminBaseController
{
    /**
     * 用户分类管理
     * @adminMenu(
     *     'name'   => '用户分类管理',
     *     'parent' => 'default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '用户分类管理',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();
        $admin_id = cmf_get_current_admin_id();
        $admin = Db::name('user')->where('id',$admin_id)->find();
        
        $tenent_id = $admin['tenant_id']==0?$admin_id:$admin['tenant_id'];
        $role = Db::name("role_user")->where("user_id", $admin_id)->where('role_id','in',[1,2])->select()->toArray();

        $list = Db::name('tenant_user_category')->alias('u')
            ->where('status',1)
            ->where(function(Query $query) use ($admin_id,$tenent_id){
                if($admin_id != 1 && empty($role)){
                    $query->where('tenant_id',$tenent_id);
                }
            })
            ->order('id desc')
            ->paginate(10);

        $list->appends($param);

        $this->assign('list',$list);
        // 获取分页显示
        $page = $list->render();
        $this->assign("page", $page);

        $this->assign('type','user');

        return $this->fetch();
    }
    /**
     * 删除用户分类
     * @adminMenu(
     *     'name'   => '删除用户分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '删除用户分类',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $id = $this->request->param('id',0,'intval');

        if($id){
            $res = Db::name('tenant_user_category')->where('id',$id)->update(['status'=>0]);
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');

            }
        }
    }
    /**
     * 添加用户分类
     * @adminMenu(
     *     'name'   => '添加用户分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '添加用户分类',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $this->assign('type','user');

        return $this->fetch();
    }

    /**
     * 商户客户分类添加提交
     * @adminMenu(
     *     'name'   => '商户客户分类添加提交',
     *     'parent' => 'add',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '商户客户分类添加提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param('post');
            $admin_id = cmf_get_current_admin_id();
            $admin = Db::name("user")->where("id", $admin_id)->find();
            $user = [
                'name'=>$param['name'],
                'tenant_id'=>$admin['tenant_id']==0?$admin_id:$admin['tenant_id']
            ];
             $res = Db::name('tenant_user_category')->insertGetId($user);

             if($res){
                $this->success("添加成功","add");
             }else{
                $this->error("添加失败");
             }
             
        }
    }
    /**
     * 员工分类管理
     * @adminMenu(
     *     'name'   => '员工分类管理',
     *     'parent' => 'default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '员工分类管理',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function staff()
    {
        $param = $this->request->param();
        $list = Db::name('tenant_staff_category')
            ->where('status',1)
            ->order('id desc')
            ->paginate(10);
        $list->appends($param);

        $this->assign('list',$list);
        // 获取分页显示
        $page = $list->render();
        $this->assign("page", $page);

        $this->assign('type','staff');

        return $this->fetch('index');
    }
    /**
     * 添加员工分类
     * @adminMenu(
     *     'name'   => '添加员工分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '添加员工分类',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function addStaffCategory()
    {
        $this->assign('type','staff');
        return $this->fetch('add');
    }

    /**
     * 商户员工分类添加提交
     * @adminMenu(
     *     'name'   => '商户员工分类添加提交',
     *     'parent' => 'addStaff',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '商户员工分类添加提交',
     *     'param'  => ''
     * )
     */
    public function addStaffPost()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param('post');
            $admin_id = cmf_get_current_admin_id();
            $admin = Db::name("user")->where("id", $admin_id)->find();
            $user = [
                'name'=>$param['name'],
                'tenant_id'=>$admin['tenant_id']==0?$admin_id:$admin['tenant_id']
            ];
             $res = Db::name('tenant_staff_category')->insertGetId($user);

             if($res){
                $this->success("添加成功",'addStaffCategory');
             }else{
                $this->error("添加失败");
             }
             
        }
    }
}